package puziran.sofa.util;

import java.io.IOException;
import java.io.PrintStream;
import java.util.StringTokenizer;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class FilterServlet extends HttpServlet
  implements Filter
{
  private FilterConfig filterConfig;
  private String[] freePages;
  private String toPage = null;

  public void init(FilterConfig filterConfig)
    throws ServletException
  {
    int i = 0;
    String pages = null;
    StringTokenizer strTokenizer = null;

    this.filterConfig = filterConfig;

    this.toPage = filterConfig.getInitParameter("toPage");
    pages = filterConfig.getInitParameter("freePages");
    if ((this.toPage != null) && (pages != null) && (this.toPage.trim().length() != 0)) pages.trim().length();

    strTokenizer = new StringTokenizer(pages, ";");
    this.freePages = new String[strTokenizer.countTokens()];
    while (strTokenizer.hasMoreTokens()) {
      this.freePages[(i++)] = strTokenizer.nextToken();
    }

    if (!isFreePage(this.toPage))
      System.out.println("初始化：web.xml中filter初始化参数\"toPage\"的值必须是\"freePage\"中的某个页面.");
  }

  private boolean isFreePage(String requestURI)
  {
    boolean isFree = false;

    for (int i = 0; i < this.freePages.length; i++) {
      if (requestURI.endsWith(this.freePages[i]))
      {
        return true;
      }
    }

    return isFree;
  }

  private boolean isValidSession(ServletRequest request)
  {
    HttpServletRequest httpRequest = (HttpServletRequest)request;
    HttpSession session = httpRequest.getSession(false);

    return (session != null) && (session.getAttribute("partyname") != null);
  }

  public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
  {
    String requestURI = null;
    HttpServletRequest httpRequest = (HttpServletRequest)request;
    HttpServletResponse httpResponse = (HttpServletResponse)response;
    requestURI = httpRequest.getRequestURI();

    if ((!isFreePage(requestURI)) && 
      (!isValidSession(request))) {
      System.out.println("请求URI=" + requestURI + " session非法");
      String toPageURL = null;
      try {
        toPageURL = httpRequest.getContextPath() + this.toPage;
        httpResponse.encodeRedirectURL(toPageURL);
        httpResponse.sendRedirect(toPageURL);
      } catch (IOException ex) {
        ex.printStackTrace();
      }
    }

    if (!httpResponse.isCommitted())
      try {
        filterChain.doFilter(request, response);
      } catch (ServletException sx) {
        this.filterConfig.getServletContext().log(sx.getMessage());
      } catch (IOException iox) {
        this.filterConfig.getServletContext().log(iox.getMessage());
      }
  }

  public void destroy()
  {
  }

  public FilterConfig getFilterConfig() {
    return this.filterConfig;
  }

  public void setFilterConfig(FilterConfig filterConfig) {
    this.filterConfig = filterConfig;
  }
}